/**
 * Created by Administrator on 2018-07-16.
 */
define(function (require,exports,module) {
    var sTpl = require("./certificateMgr.html");
    require("./certificateMgr.css");
    var VueComponent = Vue.extend({
        template: sTpl,
        data:function () {
            return {
                uploadFormVisible:false,
                checkFileFormVisible:false,
                fileList: [],
                uploadDate:{
                    fileList: []
                },
                fwUserPos:[],
                tableData: {},
                fileData:[],
                certificateMgrPo:{},
                title:'',
                title1:'',
                checkFormInline:{
                    userCnName:'',
                    idNumber:''
                },

                certificateFormInline:{
                    fwUserPo:{userId:''},
                    idNumber:'',
                    certificate:'',
                    certificateAuth:'',
                    certificateDate:'',
                    certificateNum:'',
                    certificateIndate:'',
                    file:'',
                    remarks:''
                },
                pickerOptions1: {
                    shortcuts: [{
                        text: '今天',
                        onClick:function(picker) {
                            picker.$emit('pick', new Date());
                        }
                    }, {
                        text: '昨天',
                        onClick:function(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24);
                            picker.$emit('pick', date);
                        }
                    }, {
                        text: '一周前',
                        onClick:function (picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', date);
                        }
                    }]
                }
            }
        },
        methods:{
            delectCertificateFile:function (scope) {
                var $this = this ;
                this.$confirm('确定删除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'success'
                }).then(function() {
                    axios.post(basepath+'/api/flightCrewMgr/deleteCertificateFile',scope.row)
                        .then(function (response) {
                            $this.checkCertificate();
                            $this.fileData.splice(scope.row,1);
                            $this.$notify({
                                title: '已删除',
                                message: '已删除'
                            });
                        });
                })
            },
            getFwUser:function () {
                var $this = this;
                axios.get(basepath+"/api/webframe/fAllFwUser"
                ).then(function (response) {
                    $this.fwUserPos=response.data;
                });
            },
            checkFile:function (scope) {
              this.fileData=scope.row.certificateFilePos;
              this.checkFileFormVisible=true;
            },
            uploadCertificate:function (scope) {
                this.fileList=[];
                this.uploadDate.fileList=[];
                this.certificateMgrPo=scope.row;
                this.uploadFormVisible=true;
            },
            submitUpload:function() {
                var $this=this;
                this.$refs.certificateUpload.validate(function(val){
                    if(val){
                        if($this.fileList==''){
                            $this.$notify({"title":"未选取文件",message:"请先选取文件"});
                            return;
                        }
                        $this.$refs.upload.submit();
                    }
                })
            },
            uploaded:function(file, fileList) {
                file.certificateMgrPo=this.certificateMgrPo;
                var $this=this;
                axios.post(basepath+'/api/flightCrewMgr/saveCertificateFile',file)
                    .then(function (response) {
                        $this.findAllCertificateMgrPo();
                        $this.uploadFormVisible=false;
                        $this.$notify({
                            title:"上传成功",
                            message:file.fileName+"文件上传成功"
                        });

                    });
            },
            handleRemove:function(file, fileList) {
                console.log(file, fileList);
            },
            handlePreview:function(file) {
                console.log(file);
            },
            handleChange:function(file, fileList) {
                this.fileList=fileList;
            },
            addCertificate:function () {
                this.$refs.baseTable.dialogFormVisible = true;
                this.certificateFormInline={
                    fwUserPo:{userId:''},
                        idNumber:'',
                        certificate:'',
                        certificateAuth:'',
                        certificateDate:'',
                        certificateNum:'',
                        certificateIndate:'',
                        file:'',
                        remarks:''
                },
                    this.title='添加成功';
                    this.title1='添加失败';
            },
            findAllCertificateMgrPo:function() {
                var $this = this;
                axios.get(basepath+"/api/flightCrewMgr/findAllCertificateMgrPo"
                ).then(function (response) {
                    $this.tableData=response.data;
                });
            },
            saveCertificate:function () {
                var $this = this;
                this.$refs.certificateFormInline.validate(function(validate){
                    if(validate){
                        axios.post(basepath+'/api/flightCrewMgr/saveCertificate',$this.certificateFormInline)
                            .then(function (response) {
                                $this.$refs["certificateFormInline"].resetFields();
                                $this.findAllCertificateMgrPo();
                                $this.$notify({
                                    title: $this.title,
                                    message: $this.title
                                });

                            });

                        $this.$refs.baseTable.dialogFormVisible = false;

                    }else{
                        $this.$notify({
                            title: $this.title1,
                            message: $this.title1
                        });
                        return  false;
                    }
                });

            },
            checkCertificate:function () {
                var $this = this;
                axios.get(basepath+"/api/flightCrewMgr/checkCertificate",{
                    params: {
                        userCnName:$this.checkFormInline.userCnName,
                        idNumber:$this.checkFormInline.idNumber
                    }}
                ).then(function (response) {
                    console.log(response);
                    $this.tableData=response.data;
                });
            },
            editCertificate:function (scope) {
                this.$refs.baseTable.dialogFormVisible = true;
                this.certificateFormInline=scope.row;
                this.title='修改成功';
                this.title1='修改失败';
            },
            deleteCertificate:function (scope) {
                var $this = this ;
                this.$confirm('确定删除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'success'
                }).then(function() {
                    axios.post(basepath+'/api/flightCrewMgr/deleteCertificate',scope.row)
                        .then(function (response) {
                            $this.checkCertificate();
                            $this.$notify({
                                title: '已删除',
                                message: '已删除'
                            });
                        });
                })
            },
            clearCertificate:function () {
                this.$refs.baseTable.dialogFormVisible = false;
            }
        },
        watch:{
        },
        created:function () {
            this.getFwUser();
            this.findAllCertificateMgrPo();
        },
        activated:function () {
            messages.$emit('change','资质证书管理');
        }
    })
    module.exports=VueComponent;
    module.exports.reDefine=function (val) {
        module.exports.type=val;
        return module.exports;
    }

})

